﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace INSTA.Exercices.Comparable
{
    class Program
    {
        static void Main(string[] args)
        {
            Test_IComparable_Generic();

            //Test_YearOfBirthComparer_Generic();

            

        }

        public static void Test_IComparable_Generic()
        {
            Person[] persons = new Person[3];

            persons[0]  = new Person() {Name = "dfsfdw"};
            persons[1] = new Person() {Name = "azeteztye"};
            persons[2] = new Person() {Name = "bhvfghdf"};

            // On trie le tableau en se basant sur l'implémentation
            // de l'interface IComparable implémentée dans Person.
            Array.Sort(persons);

            //foreach(var p in persons)
            //{
            //    Console.WriteLine(p.Name);
            //}

            PrintArray(persons);

        }


        public static void Test_YearOfBirthComparer_Generic()
        {
            Person[] persons = new Person[3];

            persons[0] = new Person() {YearOfBirth = 2014};
            persons[1] = new Person() {YearOfBirth = 1983};
            persons[2] = new Person() {YearOfBirth = 789};

            // On fournit maintenant notre objet qui implémente
            // IComparer<Person> comme argument de la méthode Array.Sort
            Array.Sort(persons, new YearOfBirthComparer());

            //foreach (var p in persons)
            //{
            //    Console.WriteLine(p.YearOfBirth);
            //}
            PrintArray(persons);
        }

        public static void PrintArray(IEnumerable<Person> tab )
        {
            foreach (var x in tab)
            {
                Console.WriteLine(x.YearOfBirth);
                Console.WriteLine(x.Name);
                Console.WriteLine();

            }
        }
        
 


    }
}
